import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { HttpService } from '@core/services/http.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import swal, { SweetAlertType } from 'sweetalert2';


@Component({
    selector: 'app-pro-list',
    templateUrl: './pro-list.component.html',
    styleUrls: ['./pro-list.component.less']

})
export class ProListComponent implements OnInit {

    isActive: boolean;
    list: any = [];
    pageSize: number;
    pageIndex = 1;
    ident: string;
    IP_PORT = this.Http.IP_PORT;
    total: number;
    token: string;
    style: any = {
        width: '70%'
    };
    loading: false;
    showClass = false;
    _loading = false;
    classList = [];
    courseList = [];
    P_ID: string;
    CLA_ID: string;
    P_TYPE: string;
    user_id = JSON.parse(localStorage.getItem('_user')).user_id;
    quoteToMain(p_id) {
        // console.log(p_id);
        // console.log(this.token);
        this.confirmServ.confirm({
            title: '是否要添加到我的项目?',
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
                this.Http.addMySubjuct({ p_id: p_id }).subscribe(data => {
                    swal(`${data['msg']}`, '', 'info');
                })
            },
            onCancel() {

            }
        });

    }
    constructor(
        private Http: HttpService,
        private modal: NzModalService,
        private msg: NzMessageService,
        private confirmServ: NzModalService,
        private ActiveRoute: ActivatedRoute,
        public router: Router) {

    }

    preview(p_id, isCloud, isUse) {
        this.router.navigate(['../../advance'], { relativeTo: this.ActiveRoute, queryParams: { p_id: p_id, isCloud: isCloud, isUse: isUse } });
    }


    ngOnInit() {
        this.ActiveRoute.params.subscribe(res => {
            this.ident = res.ident;
            this.getData();
        });
    }

    getData() {
        if (this.ident === 'private') {
            this.isActive = true;
            this.pageSize = 7;
            this.Http.getProject('private', { pi: this.pageIndex, ps: this.pageSize }).then(data => {
                this.list = [];
                this.list.push({
                    'p_name': '',
                    'p_image': ''
                });
                this.list = this.list.concat(data.list);
                this.total = data.total;
            });
        } else {

            this.isActive = false;
            this.pageSize = 8;
            this.Http.getProject('cloud', { pi: this.pageIndex, ps: this.pageSize }).then(data => {
                //console.log(data)
                this.list = [];
                this.list = data.list;
                this.total = data.total;
                this.Http.getCloudByTechId({ u_id: this.user_id }).subscribe(res => {
                    //console.log(res)
                    for (let ylist of this.list) {
                        ylist['isUse'] = false;
                        for (let klist of res['list']) {
                            if (ylist.p_id == klist.cloud_id) {
                                ylist.isUse = true
                            }
                        }
                    }
                })

            });
        }

    }


    deleteProjectById(id) {
        this.confirmServ.confirm({
            title: '是否要删除该项目?',
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
                this.Http.deleteProjectById({ p_id: id, isCloud: false }).subscribe(res => {
                    swal(`${res['msg']}`, '', 'info');
                    this.getData();
                });

            },
            onCancel() {

            }
        });

    }

    showList(item) {
        //   console.log(item);
        this.P_TYPE = item.p_type;
        this.showClass = true;
        this._loading = true;
        this.P_ID = item.p_id;
        this.Http.getClassListByPid({ p_id: item.p_id }).subscribe(res => {

            this.classList = res.list;
            //console.log(res.list);
            if (res.list != "") {
                this.showCourList(res.list[0].cla_id);
            } else {
                this.courseList = [];
                this._loading = false;
            }
        })
    }

    showCourList(cla_id) {
        this._loading = true;
        this.CLA_ID = cla_id;
        this.Http.getCourseListById({ p_id: this.P_ID, cla_id: cla_id }).subscribe(res => {
            // console.log(res);
            this.courseList = res.list;
            this._loading = false;
        })
    }

    play(i) {
        let cla_id = this.CLA_ID;
        let p_id = this.P_ID;
        let p_type = JSON.parse(this.P_TYPE).value;
        let cour_id = i.cour_id;
        let token = JSON.parse(localStorage.getItem('_user')).access_token;
        //  console.log('cid---'+ cla_id + 'pid----' + p_id +'p_type---' + p_type + 'courid----'+ cour_id);
        this.confirmServ.confirm({

            title: '您是否确定演练该项目',
            okText: '确定',
            cancelText: '取消',
            onOk: () => {

                this.Http.quickEntryProject({ cla_id: cla_id, p_type: p_type, cour_id: cour_id, day: '1', exercisetypes: '0', p_id: p_id, deviceType: 'pc', token: token }).subscribe(data => {
                    // console.log(data);

                })


            },
            onCancel() {

            }
        });
    }
}
